package com.maven.io.BIO;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;


/**
 * @Author YongQiang
 * @Date 2021/8/17 9:13
 * @Version 1.0
 */
public class BioServer {
    private static Logger log = LoggerFactory.getLogger(BioServer.class);
    private static int PORT = 8086;
    public static void main(String[] args) throws IOException {
        // 不是为了成为Socket，而是为了当有人与它建立连接时产生一个Socket
        ServerSocket serverSocket = new ServerSocket(PORT);
        log.info("服务端启动,监听端口:{}",PORT);
        try {
            Socket socket = serverSocket.accept();
            try {
                System.out.println("客户端连接建立");
                InputStream in = socket.getInputStream();
                OutputStream out = socket.getOutputStream();
                int available = in.available();
                byte[] bytes = new byte[available];
                in.read(bytes, 0, available);
                System.out.println(new String(bytes));
                out.write(bytes);
            }finally {
                socket.close();
            }
        }finally {
            serverSocket.close();
        }


    }
}
